]>
Commit | Line | Data |
---|---|---|
5802c153 RBR |
1 | /* |
2 | Copyright (C) 2024 Rubén Beltrán del Río | |
3 | ||
4 | This program is free software: you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation, either version 3 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program. If not, see https://captura.tranquil.systems. | |
16 | */ | |
f5d16c1c RBR |
17 | import CoreGraphics |
18 | ||
19 | extension CGImage { | |
20 | func resize(by scale: CGFloat) -> CGImage? { | |
21 | let width = Int(CGFloat(self.width) / scale) | |
22 | let height = Int(CGFloat(self.height) / scale) | |
23 | ||
24 | let bitsPerComponent = self.bitsPerComponent | |
25 | let colorSpace = self.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)! | |
26 | let bitmapInfo = self.bitmapInfo.rawValue | |
27 | ||
28 | guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo) else { | |
29 | return nil | |
30 | } | |
31 | ||
32 | context.interpolationQuality = .high | |
33 | context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height)) | |
34 | ||
35 | return context.makeImage() | |
36 | } | |
37 | } |